package com.link.gateway.base.dutyinfo.service;

import com.link.gateway.core.redis.redisclient.LinkRedisClient;
import com.link.gateway.core.utils.RedisUtil;
import com.link.gateway.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author HuangLJ
 * @date 2019年07月23日
 */
@Service
public class DutyInfoServiceImpl implements DutyInfoService {

    /**
     * 把默认的url信息写入到全局变量里面去
     *
     * @author huanglongjian
     * @date 2019-02-25 17:10
     */
    @Override
    public Map<String, List<String>> setDutyUsernameToCache() throws Exception {
        LinkRedisClient jedis = null;
        Map<String, List<String>> dutyUsername = new HashMap<>();
        try {
            jedis = RedisUtil.getJedis();
            Set<String> dutyIds = jedis.hkeys("duty_username");
            List<String> list = new ArrayList<>();
            for (String dutyId : dutyIds) {
                String userNames = jedis.hget("duty_username", dutyId);
                String[] userNameArray = userNames.split(",");
                for (String userName : userNameArray) {
                    if (StringUtils.isNotBlank(userName)) {
                        list.add(userName);
                    }
                }
                dutyUsername.put(dutyId, list);
            }
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return dutyUsername;
    }

    /**
     * 把默认的url信息写入到缓存中去
     *
     * @author huanglongjian
     * @date 2019-02-25 17:10
     */
    @Override
    public List<String> setDutyUrlToCache(String rediskey) throws Exception {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            return jedis.lrange(rediskey, 0, -1);

        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * 把所有的权限信息加载到内存中
     *
     * @author huanglongjian
     * @date 2019-02-25 17:10
     */
    @Override
    public void setDutyPermissionToCache(String key) throws Exception {

        //加载职责Url
        this.setDutyUrlToCache(key);
    }
}
